<template>
	<view>
		<view class="wrap">
			<view class="u-tabs-box">
				<u-tabs-swiper activeColor="#f29100" ref="tabs" :list="list" :current="current" @change="change"
					:is-scroll="false" swiperWidth="750"></u-tabs-swiper>
			</view>
			<swiper class="swiper-box" :current="swiperCurrent" @transition="transition"
				@animationfinish="animationfinish">
				<swiper-item v-for="(item,index) in list" :key='index' class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;">
						<view class="page-box">
							<view class="order" v-for="(res, index) in orderList[index]" :key="index">
								<view class="top">
									<view class="left">
										<u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon>
										<view class="store">{{ res.username }}</view>
										<u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
									</view>
									<view style="margin-left: 6px;" class="right">{{ res.phone }}</view>
								</view>
								<view>
									{{res.address}}
								</view>
								<view class="item" v-for="(item, index) in res.orderGoods" :key="index">
									<view class="left">
										<image :src="item.goodsImage.split(',')[0]" mode="aspectFill"></image>
									</view>
									<view class="content">
										<view class="title u-line-2">{{ item.goodsName }}</view>
										<view class="type">{{ item.specsName }}/{{item.goodsUnit}}</view>
									</view>
									<view class="right">
										<view class="price">
											{{ item.price }}元
										</view>
										<view class="number">x{{ item.num }}</view>
									</view>
								</view>
								<view class="total">
									共{{ res.orderGoods.length }}件商品 合计:
									<text class="total-price">
										{{ res.price}}元
									</text>
								</view>
								<view class="bottom">
									<view @click="cancel(res,index)" v-if="res.status == '0'" class="exchange btn">取消订单
									</view>
									<view @click="confirmOrder(res,index)" v-if="res.status == '1'" class="exchange btn">确认收货
									</view>
									<view @click="comment(res)" v-if="res.status == '2'" class="evaluate btn">评价</view>
								</view>
							</view>
						</view>
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>
		<u-modal confirm-color="lightgreen" :content-style="{padding : '10px'}" :show-cancel-button="true" title="评论"
			:zoom="false" v-model="show" @confirm="confirmComment" @cancel="cancelComment">
			<view style="height: 100px;">
				<textarea v-model="commentText" placeholder="请输入评论" auto-height />
			</view>
		</u-modal>
	</view>
</template>

<script setup>
	import {
		onLoad
	} from '@dcloudio/uni-app';
	import {
		ref,
		computed,
	} from 'vue';
	import {
		getOrderListApi,
		cancelOrederApi,
		confirmOrederApi
	} from '../../api/order';
	import {
		addCommentApi
	} from '../../api/user';
	//评论弹框显示
	const show = ref(false)
	const orderId = ref('')
	const commentText = ref('')
	//确认收货
	const confirmOrder = (item,index) =>{
		uni.showModal({
			title: '提示',
			content: '确定收货吗',
			success: async function(result) {
				if (result.confirm) {
					let result = await confirmOrederApi({
						orderId: item.orderId
					})
					if (result && result.code == 200) {
						getOrderList()
						change(index)
					}
				} else if (result.cancel) {}
			}
		})
	}
	//评论点击
	const comment = (item) => {
		commentText.value = ''
		orderId.value = item.orderId
		show.value = true
	}
	//评论提交
	const confirmComment = async (item) => {
		if (!commentText.value) {
			uni.showToast({
				icon: 'none',
				title: '请填写评论'
			})
		}
		let result = await addCommentApi({
			orderId: orderId.value,
			openId: uni.getStorageSync('openid'),
			commentText: commentText.value,
		})
		if (result && result.code == 200) {
			show.value = false
		}
	}
	//评论取消
	const cancelComment = () => {
		show.value = false
	}
	const currentPage = ref(1)
	const pageSize = ref(3)
	const pages = ref(0)
	const tabs = ref(null)
	const currents = ref(0)
	const swiperCurrent = ref(0)
	const tabsHeight = ref(0)
	const dx = ref(0)
	const orderList = ref([
		[],
		[],
		[],
		[]
	])
	const list = ref([{
			name: '全部'
		},
		{
			name: '待发货'
		},
		{
			name: '已发货'
		},
		{
			name: '已收货',
		}
	])
	const current = ref(0)
	const totalPrice = (item) => {
		let price = 0;
		item.map(val => {
			price += parseFloat(val.price);
		});
		return price.toFixed(2);
	}
	const totalNum = (item) => {
		let num = 0;
		item.map(val => {
			num += val.number;
		});
		return num;
	}
	//查询订单
	const getOrderList = async () => {
		let result = await getOrderListApi({
			openId: uni.getStorageSync('openid'),
		})
		console.log(result)
		if (result && result.code == 200) {
			orderList.value[0] = result.data.all
			orderList.value[2] = result.data.statusOne
			orderList.value[3] = result.data.statusTwo
			orderList.value[1] = result.data.statusZero
		}
	}
	// tab栏切换
	const change = (index) => {
		swiperCurrent.value = index;
	}
	const transition = ({
		detail: {
			dx
		}
	}) => {
		tabs.value.setDx(dx);
	}
	const animationfinish = ({
		detail: {
			current
		}
	}) => {
		tabs.value.setFinishCurrent(current);
		swiperCurrent.value = current;
	}
	//取消订单
	const cancel = (item, index) => {
		uni.showModal({
			title: '提示',
			content: '确定取消订单吗',
			success: async function(result) {
				if (result.confirm) {
					let result = await cancelOrederApi({
						orderId: item.orderId
					})
					if (result && result.code == 200) {
						getOrderList()
						change(index)
					}
				} else if (result.cancel) {}
			}
		})
	}
	onLoad(() => {
		getOrderList();
	})
</script>

<style>
	/* #ifndef H5 */
	page {
		height: 100%;
		background-color: #f2f2f2;
	}

	/* #endif */
</style>

<style lang="scss" scoped>
	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 10rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.top {
			display: flex;

			.left {
				display: flex;
				align-items: center;

				.store {
					margin: 0 10rpx;
					font-size: 32rpx;
					font-weight: bold;
				}
			}

			.right {
				font-size: 32rpx;
				color: $u-type-warning-dark;
			}
		}

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.left {
				margin-right: 20rpx;

				image {
					width: 200rpx;
					height: 150rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				.title {
					font-size: 28rpx;
					line-height: 50rpx;
				}

				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}

				.delivery-time {
					color: #e5d001;
					font-size: 24rpx;
				}
			}

			.right {
				margin-left: 20rpx;
				padding-top: 20rpx;
				text-align: right;

				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;

			.total-price {
				font-size: 32rpx;
			}
		}

		.bottom {
			display: flex;
			margin-top: 40rpx;
			padding: 0 10rpx;
			justify-content: flex-end;
			align-items: center;

			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-type-info-dark;
			}

			.exchange {
				color: red;
				border-color: red;
			}

			.evaluate {
				color: $u-type-warning-dark;
				border-color: $u-type-warning-dark;
			}
		}
	}

	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;

		image {
			width: 164rpx;
			height: 164rpx;
			border-radius: 50%;
			margin-bottom: 20rpx;
		}

		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
		}
	}

	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
	}

	.swiper-box {
		flex: 1;
	}

	.swiper-item {
		height: 100%;
	}
</style>